<template>
  <detail-dialog modal showReset :title="title" :show.sync="show" @reset="resetForm" @update:show="updateShow" :top="5" @save="save">
    <el-form :model="editOrderInfo" label-width="140px" :rules="rules" ref="orderInfoForm">
      <el-form-item label="联系人" prop="returnContractsName" :rules="{ required: true, message: '不能为空', trigger: 'blur' }">
        <el-input style="width: 90%" v-if="editOrderInfo.returnOrderType===1" clearable v-model="editOrderInfo.returnContractsName" placeholder="联系人"></el-input>
        <PopoverSearch
          v-else
          width="90%"
           @hide="selectContracts"  placeholder="+添加联系人"
           type='customerSigner' :showMoreBtn="false" :params="{customerCode:editOrderInfo.relationCode}"  :disabled="!(editOrderInfo.relationCode && editOrderInfo.relationCode!==null)"   v-model="editOrderInfo.returnContractsCode" valueKey="contactsCode" lableKey="contactsName"
          :name.sync="editOrderInfo.returnContractsName"/>
        </el-form-item>
        <el-form-item label="联系人地址" prop="returnAddress">
              <el-input
              style="width: 90%"
                v-model="editOrderInfo.returnAddress"
                placeholder="联系人地址" clearable
              ></el-input>
            </el-form-item>
        <el-form-item label="联系人手机" prop="returnContractsMobile">
              <el-input
              style="width: 90%"
                v-model="editOrderInfo.returnContractsMobile"
                placeholder="联系人手机"
                maxlength="11"
              ></el-input>
      </el-form-item>
            <el-form-item label="联系人邮箱" prop="returnContractsEmail">
              <el-input
                style="width: 90%"
                v-model="editOrderInfo.returnContractsEmail"
                placeholder="联系人邮箱" clearable
              ></el-input>
            </el-form-item>
    </el-form>
  </detail-dialog>
</template>
<script>
import DetailDialog from '@/components/DetailDialog'
import PopoverSearch from '@/views/components/popoverSearch'
import returnOrder from '@/api/eims/returnOrderAPI'
export default {
  name: 'order_ads',
  props: {
    title: {type: String, default: '编辑退货地址信息'},
    visible: {
      type: Boolean,
      default: false
    },
    param: {type: Object,
      default: () => {
        return {}
      }}
  },
  components: { DetailDialog, PopoverSearch },
  watch: {
    param: {
      handler (nVal, oVal) {
        this.originalOrderInfo = {...nVal}
      },
      deep: true,
      immediate: true
    },
    visible: {
      handler (val) {
        this.show = val
        this.editOrderInfo = {...this.param}
      },
      immediate: true
    },
    show (nVal) {
      this.$emit('update:visible', nVal)
    }
  },
  data () {
    return {
      editOrderInfo: {},
      originalOrderInfo: {},
      show: this.visible,
      salesOrderStatus: [],
      rules: {
        returnAddress: [{ required: true, message: '不能为空', trigger: 'blur' }],
        returnContractsMobile: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    selectContracts (data) {
      if (data.contactsCode !== this.editOrderInfo.returnContractsCode) {
        this.$set(this.editOrderInfo, 'returnAddress', data.address)
        this.$set(this.editOrderInfo, 'returnContractsEmail', data.email)
        this.$set(this.editOrderInfo, 'returnContractsMobile', data.mobile)
      }
    },
    save () {
      this.$refs.orderInfoForm.validate(valid => {
        if (valid) {
          const params = {}
          params.returnNo = this.editOrderInfo.returnNo
          params.returnType = this.editOrderInfo.returnType
          params.returnDate = this.editOrderInfo.returnDate
          params.chargePerson = this.editOrderInfo.chargePerson
          params.chargePersonName = this.editOrderInfo.chargePersonName
          params.returnAddress = this.editOrderInfo.returnAddress
          params.returnContractsEmail = this.editOrderInfo.returnContractsEmail
          params.returnContractsMobile = this.editOrderInfo.returnContractsMobile
          params.returnContractsName = this.editOrderInfo.returnContractsName
          params.returnContractsCode = this.editOrderInfo.returnContractsCode
          returnOrder.updateReturnOrderInfoAPI(params).then(res => {
            if (res.code === 0) {
              this.show = false
              this.$emit('saveAfter', this.editOrderInfo.returnNo)
            }
          })
        }
      })
    },
    updateShow (val) {
      this.show = val
    },
    resetForm () {
      this.editOrderInfo = {...this.originalOrderInfo}
    //   this.$refs.orderInfoForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
